package com.yc.chatroom.common.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

import javax.crypto.spec.IvParameterSpec;
import java.nio.charset.StandardCharsets;
import java.security.spec.AlgorithmParameterSpec;

/**
 * 密码加密
 */
public class Des3Util {

    private Des3Util() {
    }

    public static String decode(String data) {
        SymmetricCrypto des = getSymmetricCrypto();
        AlgorithmParameterSpec iv = new IvParameterSpec("00000000".getBytes());
        des.setParams(iv);
        String s;
        s = new String(des.decrypt(Base64.decode(data)), StandardCharsets.UTF_8);
        return s;
    }

    public static String encode(String data) {
        SymmetricCrypto des = getSymmetricCrypto();
        AlgorithmParameterSpec iv = new IvParameterSpec("00000000".getBytes());
        des.setParams(iv);
        String s;
        s = Base64.encode(des.encrypt(data.getBytes(StandardCharsets.UTF_8)));
        return s;
    }

    private static SymmetricCrypto getSymmetricCrypto() {
        return new SymmetricCrypto("DESede/CBC/PKCS5Padding", "684523174589651002354157".getBytes(StandardCharsets.UTF_8));
    }
}
